//
//  SearchResultCell.m
//  CandyCrushClone
//
//  Created by Petar on 2/25/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "SearchResultCell.h"

@interface SearchResultCell ()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *addButton;


@end

@implementation SearchResultCell
{
    
    NSIndexPath *_indexPath;
    
}

- (void)awakeFromNib
{
    // Initialization code
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (IBAction)didPressAddButton:(id)sender
{
    
    if ( self.delegate && [self.delegate respondsToSelector:@selector(didPressAddButtonAtIndexPath:)] ) {
        [self.delegate didPressAddButtonAtIndexPath:_indexPath];
    }
    
}


- (void)configureWithName:(NSString *)username andBuddyList:(NSMutableArray *)buddyListNames andMyUsername:(NSString *)myUsername forIndexPath:(NSIndexPath *)indexPath
{
    
    _nameLabel.text = username ? username : @"";
    
    BOOL shouldHideAddButton = NO;
    
    for (NSString *buddyName in buddyListNames) {
        
        // Hide Add button if username is already my Buddy or this is Me
        if ( [buddyName isEqualToString:username] || [username isEqualToString:myUsername] ) {
            shouldHideAddButton = YES;
            break;
        }
    }
    
    if ( shouldHideAddButton == YES ) {
        _addButton.hidden = YES;
    } else {
        _addButton.hidden = NO;
    }
    
    _indexPath = indexPath;
    
}

@end
